<?php 
$this->lang->load('admin_ad', $this->session->admin_language);
$this->load->view('admin/layout/head', array('title'=>$this->lang->line('ad')), FALSE);
?>

<div id="container">
<ul class="nav nav-tabs" id="subject-tab">
	<li class="active"><a href="javascript:;"><?php echo isset($ad)?$this->lang->line('edit'):$this->lang->line('add') ?></a></li>
</ul>
<form class="form-inline deal-from container-fluid no-padding" id="ad-form">
	<input type="hidden" name="id" value="<?php echo isset($ad['id'])?$ad['id']:0 ?>">
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('title') ?></label>
		<div class="form-group">
			<input type="text" name="title" class="form-control" value="<?php echo isset($ad['title'])?$ad['title']:''?>" style="width: 600px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('link') ?></label>
		<div class="form-group">
			<input type="text" name="url" class="form-control" value="<?php echo isset($ad['url'])?$ad['url']:''?>" style="width: 600px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('category') ?></label>
		<div class="form-group">
			<?php 
			$i = 0;
			foreach($types as $k=>$v):
			?>
	  		<input type="radio" name="type" value="<?php echo $k?>" <?php if((isset($ad) && $k == $ad['type']) || $i==0) echo 'checked' ?>> <?php echo $v[$this->session->admin_language] ?>
	  		<?php 
	  		$i++;
	  		endforeach;
	  		?>
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('pic') ?></label>
		<div class="form-group" >
			<a href="javascript:;" class="btn btn-primary" id='up-btn'>UPLOAD</a>
			<input type="hidden" name='image' id="ad-image" value="<?php echo isset($ad['image'])?$ad['image']:''?>">
			<div id="up-container">
				<?php if(isset($ad) && $ad['image']): ?>
				<img src="<?php echo $ad['image']?>" alt="" style="max-width: 600px; max-height: 100px"/>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('sort') ?></label>
		<div class="form-group" >
			<input type="text" name="sort" class="form-control" value="<?php echo isset($ad['sort'])?$ad['sort']:'0'?>" style="width: 100px">
		</div>
	</div>
	<div class="mt-20">
		<label for=""><?php echo $this->lang->line('remark') ?></label>
		<div class="form-group">
			<input type="text" name="content" class="form-control" value="<?php echo isset($ad['content'])?$ad['content']:''?>" style="width: 600px">
		</div>
	</div>

	<div style="padding-left:110px" class="mt-20">
      <a href="javascript:;" class="btn btn-primary save"><?php echo $this->lang->line('save') ?></a>
      <a href="<?php echo $back_url ?>" class="btn btn-default cancel"><?php echo $this->lang->line('cancel') ?></a>
    </div>

	
</form>
</div>


<?php 
$js_array = array(
	'/assets/plupload/plupload.full.min.js'
	);
$this->load->view('admin/layout/footer',array('js'=>$js_array));
?>
<script type="text/javascript">

	$(function(){
		uploadImg('up-btn',function(img){
			$('#up-container').html('<img src="'+img+'" style="max-width: 600px; max-height: 100px"/>');
			$('#ad-image').val(img);
		});
	})

	$('.save').click(function(event) {
		
		
		if(!$('#ad-image').val()){
			alert('<?php echo $this->lang->line('submit_error_image') ?>');
			return;
		}

		Toast.loading();
		$.post('/admin/ad/save', $('#ad-form').serialize(), function(data, textStatus, xhr) {
			Toast.destory();
			var json = $.parseJSON(data);
			if(json.code == 1){
				alert('<?php echo $this->lang->line('submit_error') ?>');
				return;
			}
			if(json.code == 2){
				alert('<?php echo $this->lang->line('submit_error_input') ?>');
				return;
			}
			Toast.success('<?php echo $this->lang->line('submit_success') ?>',function(){
				window.location = '<?php echo $back_url ?>';
			})

		});
	});
</script>